package com.qiwx.pigeon.Utils;


import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.qiwx.pigeon.base.Constant;
import com.qiwx.pigeon.data.vo.EmployBean;

import static com.qiwx.pigeon.base.Constant.SHARE_NAME;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-11 15:28
 **/
public class SharedPreferencesUtil {


    public static void initPeriod(Context context) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        Constant.CHECK_EGG_DAYS = instance.getInt(Constant.SHARED_CHECK_EGG_DAYS, Constant.CHECK_EGG_DAYS_DEFAULT);
        Constant.CHECK_BABY_DAYS = instance.getInt(Constant.SHARED_CHECK_BABY_DAYS, Constant.CHECK_BABY_DAYS_DEFAULT);
        Constant.CHECK_KW_DAYS = instance.getInt(Constant.SHARED_CHECK_KW_DAYS, Constant.CHECK_KW_DAYS_DEFAULT);
        Constant.LAY_EGG_CYCLE = instance.getInt(Constant.SHARED_LAY_EGG_CYCLE, Constant.LAY_EGG_CYCLE_DEFAULT);
        Constant.LAY_KW_AFTER = instance.getInt(Constant.SHARED_LAY_EGG_CYCLE, Constant.LAY_KW_AFTER_DEFAULT);
    }

    public static void setPeriod(Context context, int type, int days) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);

        SharedPreferences.Editor editor = instance.edit();//获取编辑器
        switch (type) {
            case Constant.TYPE_CD:
                editor.putInt(Constant.SHARED_CHECK_EGG_DAYS, days);
                break;
            case Constant.TYPE_BABY:
                editor.putInt(Constant.SHARED_CHECK_BABY_DAYS, days);

                break;
            case Constant.TYPE_KW:
                editor.putInt(Constant.SHARED_CHECK_KW_DAYS, days);
                break;
            case Constant.TYPE_XZ:
                editor.putInt(Constant.SHARED_LAY_EGG_CYCLE, days);
                break;
            case Constant.TYPE_CXKW:
                editor.putInt(Constant.SHARED_LAY_KW_AFTER, days);
                break;
        }
        editor.commit();
        MyToast.showLong("修改成功");
    }

    public static int getPeriodValue(Context context, int type) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        switch (type) {
            case Constant.TYPE_CD:
                return instance.getInt(Constant.SHARED_CHECK_EGG_DAYS, Constant.CHECK_EGG_DAYS_DEFAULT);
            case Constant.TYPE_BABY:
                return instance.getInt(Constant.SHARED_CHECK_BABY_DAYS, Constant.CHECK_BABY_DAYS_DEFAULT);
            case Constant.TYPE_KW:
                return instance.getInt(Constant.SHARED_CHECK_KW_DAYS, Constant.CHECK_KW_DAYS_DEFAULT);
            case Constant.TYPE_XZ:
                return instance.getInt(Constant.SHARED_LAY_EGG_CYCLE, Constant.LAY_EGG_CYCLE_DEFAULT);
            case Constant.TYPE_CXKW:
                return instance.getInt(Constant.SHARED_LAY_KW_AFTER, Constant.LAY_KW_AFTER_DEFAULT);
        }
        return 1;


    }

    public static boolean getIsGoTimeByMinute(Context context) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        return instance.getBoolean("by_minute", false);
    }

    public static void setIsGoTimeByMinute(Context context, boolean isGoTimeByMinute) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        SharedPreferences.Editor editor = instance.edit();
        editor.putBoolean("by_minute", isGoTimeByMinute);
        editor.commit();
    }

    public static EmployBean getEmployBean(Context context) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        String json = instance.getString("employ_bean", null);
        if (json == null)
            return null;
        EmployBean bean = null;
        try {
            bean = new Gson().fromJson(json, EmployBean.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;

    }

    public static void setEmployBean(Context context, EmployBean bean) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        SharedPreferences.Editor editor = instance.edit();
        editor.putString("employ_bean", new Gson().toJson(bean));
        editor.commit();
    }

    public static void removeEmployBean(Context context) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        SharedPreferences.Editor editor = instance.edit();
        editor.remove("employ_bean").commit();
    }

    public static void setLoginInfo(Context context, String login_name, String pwd) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        SharedPreferences.Editor editor = instance.edit();
        editor.putString("login_name", login_name);
        editor.putString("pwd", pwd);
        editor.commit();
    }

    public static String[] getLoginInfo(Context context) {
        SharedPreferences instance = context.getSharedPreferences(SHARE_NAME, 0);
        String login_name = instance.getString("login_name", null);
        String pwd = instance.getString("pwd", null);
        return new String[]{login_name, pwd};
    }
}
